import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.regex.Pattern;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Mon Dec 16 14:25:34 GMT+08:00 2024
 */



/**
 * @author 22387
 */
public class modify_information extends JFrame {
    public modify_information() {
        initComponents();
    }


    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        user_Home u = new user_Home();
        u.setVisible(true);
    }

    private void button1MouseClicked2(MouseEvent e) {
        String gender = textField3.getText();
        String telephone = textField4.getText();
        String email = textField5.getText();

        // 验证手机号格式：数字
        if (!Pattern.matches("^[0-9]+$", telephone)) {
            JOptionPane.showMessageDialog(this, "手机号格式不正确，应为数字。");
            return;
        }
        // 验证邮箱格式
        if (!Pattern.matches("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$", email)) {
            JOptionPane.showMessageDialog(this, "邮箱格式不正确。");
            return;
        }

        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String rootPassword = "root";

        String sql = "UPDATE user_information SET gender=?, telephone=?, email=? WHERE user_ID=? ;";

        try (Connection conn = DriverManager.getConnection(url, username, rootPassword);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 假设user_ID是整数类型，如果不是，请根据实际类型调整
            stmt.setString(1, gender);
            stmt.setString(2, telephone);
            stmt.setString(3, email);
            stmt.setString(4, Lgoin_user.userid);

            int count = stmt.executeUpdate();
            System.out.println("Updated rows: " + count);

            JOptionPane.showMessageDialog(this, "修改信息成功！");
        } catch (NumberFormatException ex) {
            // 处理userId不是整数的情况
            System.err.println("Invalid user_ID: " + Lgoin_user.userid);
        } catch (SQLException ex) {
            // 处理SQL异常
            ex.printStackTrace();
        }
    }

    private void button2MouseClicked(MouseEvent e) {
        this.dispose();
        user_Home u = new user_Home();
        u.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button2 = new JButton();
        label8 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        textField3 = new JTextField();
        label6 = new JLabel();
        textField4 = new JTextField();
        label7 = new JLabel();
        textField5 = new JTextField();
        button1 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- button2 ----
        button2.setText("\u8fd4\u56de");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2, "cell 1 2");

        //---- label8 ----
        label8.setText("\u4fee\u6539\u4fe1\u606f");
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 8f));
        contentPane.add(label8, "cell 6 2 1 3");

        //---- label4 ----
        label4.setText("\u4fee\u6539\u4fe1\u606f\uff1a");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 2f));
        contentPane.add(label4, "cell 1 7");

        //---- label5 ----
        label5.setText("\u6027\u522b\uff1a");
        contentPane.add(label5, "cell 3 7");
        contentPane.add(textField3, "cell 5 7 8 1");

        //---- label6 ----
        label6.setText("\u7535\u8bdd\uff1a");
        contentPane.add(label6, "cell 3 10");
        contentPane.add(textField4, "cell 5 10 8 1");

        //---- label7 ----
        label7.setText("\u90ae\u7bb1\uff1a");
        contentPane.add(label7, "cell 3 15");
        contentPane.add(textField5, "cell 5 15 8 1");

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u4fee\u6539");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
                button1MouseClicked2(e);
            }
        });
        contentPane.add(button1, "cell 6 21");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    public static void main(String[] args) {
        modify_information modify_information = new modify_information();
        modify_information.setVisible(true);
        modify_information.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button2;
    private JLabel label8;
    private JLabel label4;
    private JLabel label5;
    private JTextField textField3;
    private JLabel label6;
    private JTextField textField4;
    private JLabel label7;
    private JTextField textField5;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
